package org.seckill.controller;

import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExecution;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillStateEnum;
import org.seckill.exception.RepeatSeckillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.exception.SeckillException;
import org.seckill.page.PageQueryBean;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-8-12.
 */
@Controller
@RequestMapping("seckill")
public class SeckillContriller {

    @Autowired
    private SeckillService seckillService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
      *@Author Jay
      *@Date 2017-8-12 21:18
      *@Description  所有商品主页
      */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(Model model) {

        PageQueryBean pageQueryBean = new PageQueryBean();

        List<Seckill> seckillList = seckillService.getSeckillList();
        model.addAttribute("seckillList",seckillList);

        return "list";
    }
    @RequestMapping(value = "/listPage",method = RequestMethod.POST)
    @ResponseBody
    public PageQueryBean list(@RequestBody PageQueryBean pageQueryBean) {

        PageQueryBean result = seckillService.getPageList(pageQueryBean);

        return result;
    }

//    @RequestMapping(value = "/list",method = RequestMethod.GET)
//   public String list() {
//        return "list2";
//    }


    /**
      *@Author Jay
      *@Date 2017-8-12 21:19
      *@Description  商品详情页
      */
    @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET)
    public String detail(@PathVariable("seckillId")Long seckillId,Model model) {
        if(seckillId == null) return "redirect:/seckill/list";

        Seckill seckill = seckillService.findSeckillById(seckillId);
        if(seckill == null) return "redirect:/seckill/list";

        model.addAttribute("seckill",seckill);

        return "detail";
    }
    /**
      *@Author Jay
      *@Date 2017-8-12 23:08
      *@Description 获取系统当前时间
      */
   //@RequestMapping(value = "/time/now",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @RequestMapping(value = "/time/now",method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> nowTime() {
        Date date = new Date();
        return new SeckillResult<Long>(true,date.getTime());
    }

    /**
      *@Author Jay
      *@Date 2017-8-13 10:03
      *@Description   暴露秒杀地址,拿到md5值
      */
    @RequestMapping(value = "/{seckillId}/exposer",method = RequestMethod.POST)
    @ResponseBody
    public SeckillResult<Exposer> exposer(@PathVariable("seckillId")Long seckillId) {
        SeckillResult<Exposer> result;

        try {
            Exposer exposer = seckillService.exportSeckillUrl(seckillId);
            result = new SeckillResult<Exposer>(true,exposer);
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            result = new SeckillResult<Exposer>(false,e.getMessage());
        }
        return result;
    }

    /**
      *@Author Jay
      *@Date 2017-8-13 10:14 mac
      *@Description  执行秒杀
      */
    @RequestMapping(value = "/{seckillId}/{md5}/execution",method = RequestMethod.POST)
    @ResponseBody
    public SeckillResult<SeckillExecution> execution(@PathVariable("seckillId") Long seckillId,
                                                     @PathVariable("md5") String md5,
                                                     @CookieValue(value = "killPhone",required = false) Long killPhone) {

        SeckillExecution seckillExecution;
        if(killPhone == null) return new SeckillResult<SeckillExecution>(false,"未注册");

        try {
            seckillExecution = seckillService.executeSeckill(seckillId, md5, killPhone);
            return  new SeckillResult<SeckillExecution>(true,seckillExecution);
        } catch (RepeatSeckillException e){
            logger.error(e.getMessage(),e);
            seckillExecution = new SeckillExecution(seckillId,SeckillStateEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExecution>(true,seckillExecution);
        }catch (SeckillCloseException e){
            logger.error(e.getMessage(),e);
            seckillExecution = new SeckillExecution(seckillId,SeckillStateEnum.END);
            return new SeckillResult<SeckillExecution>(true,seckillExecution);

        }catch (SeckillException e) {
            logger.error(e.getMessage(),e);
            seckillExecution = new SeckillExecution(seckillId,SeckillStateEnum.INNER_ERROR);
            return new SeckillResult<SeckillExecution>(true,seckillExecution);
        }

    }

}
